package cn.com.woxitech.dev.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.woxitech.dev.dao.gen.WmOrganizationAdminMapper;
import cn.com.woxitech.dev.dao.gen.WmOrganizationMapper;
import cn.com.woxitech.dev.model.gen.WmOrganization;
import cn.com.woxitech.dev.model.gen.WmOrganizationAdmin;
import cn.com.woxitech.dev.model.gen.WmOrganizationAdminExample;
import cn.com.woxitech.dev.model.gen.WmOrganizationExample;

@Service
public class OrganizationService {
	@Autowired
	WmOrganizationMapper organizationMapper;
	
	@Autowired
	WmOrganizationAdminMapper organizationAdminMapper;
	
	public WmOrganization getOrganizationByUserUuid(String userUuid) {
		WmOrganizationExample example = new WmOrganizationExample();
		example.createCriteria().andCreatorEqualTo(userUuid);
		List<WmOrganization> list = organizationMapper.selectByExample(example);
		if (list.isEmpty()) {
			return null;
		}else {
			return list.get(0);
		}
	}
	
	public WmOrganization getOrganizationByUuid(String uuid) {
		WmOrganizationExample example = new WmOrganizationExample();
		example.createCriteria().andUuidEqualTo(uuid);
		List<WmOrganization> list = organizationMapper.selectByExample(example);
		if (list.isEmpty()) {
			return null;
		}else {
			return list.get(0);
		}
	}
	
	public void insertOrganization(WmOrganization organization) {
		organizationMapper.insertSelective(organization);
	}
	
	public void updateOrganization(WmOrganization organization,String orgUuid) {
		WmOrganizationExample example = new WmOrganizationExample();
		example.createCriteria().andUuidEqualTo(orgUuid);
		organizationMapper.updateByExampleSelective(organization, example);
	}
	
	public void updateByPrimaryKey(WmOrganization organization) {
		organizationMapper.updateByPrimaryKeySelective(organization);
	}

	public WmOrganizationAdmin getOrganizationAdmin(String uuid) {
		// TODO Auto-generated method stub
		
		WmOrganizationAdminExample example = new WmOrganizationAdminExample();
		example.createCriteria().andUseruuidEqualTo(uuid);
		List<WmOrganizationAdmin> list = organizationAdminMapper.selectByExample(example);
		if (list.isEmpty()) {
			return null;
		}else {
			return list.get(0);
		}
	}

	public WmOrganization getDefaultOrganization() {
		// TODO Auto-generated method stub
		
		WmOrganizationExample e = new WmOrganizationExample();
		
		List<WmOrganization> orgs = this.organizationMapper.selectByExample(e);
		
		return orgs.get(0);
	}
}
